#include <iostream>
#include <typeinfo>

using namespace std;

template <class T>
void mySizeof() {
    cout << "sizeof(" << typeid(T).name() << ") is " << sizeof(T) << endl;
}

/**
 * sizeof
 *
 * 类型选择建议:
 * 1、当明确知道数值不可能为负时，使用无符号数
 * 2、使用int做整数运算，short通常过小，long通常和int大小一致，如果数值超过int，使用long long
 * 3、算数运算不要使用char和bool，如果需要使用一个不大的数，显示什么为signed char/unsigned char
 * 4、浮点运算使用double，float精度不够，但运算速度相差无几，通常long double的精度使用不到
 */
void sizeofTest() {
    mySizeof<bool>();

    mySizeof<char>();

    mySizeof<short>();
    mySizeof<int>();
    mySizeof<long>();
    mySizeof<long long>();

    mySizeof<float>();
    mySizeof<double>();
    mySizeof<long double>();
}

/**
 * 算数运算
 *
 * 1、有符号数和无符号数运算时，有符号数会提升为无符号数
 */
void arithTest() {
    int intMid = 15, intNeg = -15;
    unsigned uIntSmall = 10, uIntBig = 20;
    cout << uIntBig << "-" << uIntSmall << " = " << uIntBig - uIntSmall << endl;
    cout << uIntSmall << "-" << uIntBig << " = " << uIntSmall - uIntBig << endl;

    cout << intMid << "-" << uIntSmall << " = " << intMid - uIntSmall << endl;
    cout << intMid << "-" << uIntBig << " = " << intMid - uIntBig << endl;

    cout << intNeg << "+" << uIntSmall << " = " << intNeg + uIntSmall << endl;
    cout << intNeg << "+" << uIntBig << " = " << intNeg + uIntBig << endl;
}

int main(int argc, char* argv[]) {
    sizeofTest();
    arithTest();
    return 0;
}
